<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>信息列表</title>
    <link rel="shortcut icon" href="/favicon.ico">
    <link href="/public/admin/css/bootstrap.min.css?v=3.3.5" rel="stylesheet">
    <link href="/public/admin/css/font-awesome.css?v=4.4.0" rel="stylesheet">
    <link href="/public/admin/css/plugins/bootstrap-table/bootstrap-table.min.css" rel="stylesheet">
    <link href="/public/admin/css/animate.css" rel="stylesheet">
    <link href="/public/common/layui-v2.6.8/css/layui.css" rel="stylesheet">
    <link href="/public/admin/css/style.css?v=4.0.0" rel="stylesheet">
    <base target="_blank">
    <!-- 全局js -->
    <script src="/public/admin/js/jquery.min.js?v=2.1.4"></script>
    <script src="/public/admin/js/bootstrap.min.js?v=3.3.5"></script>
    <script src="/public/common/layui-v2.6.8/layui.js"></script>
    <!-- 自定义js -->

    <!-- Bootstrap table -->
<style>
#dataform{
    width: 600px;
    margin-top:100px;
    /*margin-left:100px;*/
}
.layui-form-label{
    width:110px;
}
#print_info{
   margin-left:30px;
}
</style>
</head>

<body class="gray-bg layui-layout-body">
<div class="layui-show" style="text-align:center">
    <form class="layui-form" action="" id="dataform" style="display:inline-block">
      <div class="layui-form-item">
        <label class="layui-form-label">姓名</label>
        <div class="layui-input-block">
          <input type="text" name="title" required  lay-verify="required" placeholder="" autocomplete="off" class="layui-input" id="realname">
        </div>
      </div>
      <div class="layui-form-item">
        <label class="layui-form-label">性别</label>
        <div class="layui-input-block">
          <input type="text" name="title" required  lay-verify="required" placeholder="" autocomplete="off" class="layui-input" id="gender">
        </div>
      </div>
      <div class="layui-form-item">
        <label class="layui-form-label">出生日期</label>
        <div class="layui-input-block">
          <input type="text" name="title" required  lay-verify="required" placeholder="" autocomplete="off" class="layui-input" id="birthday">
        </div>
      </div>
      <div class="layui-form-item">
        <label class="layui-form-label">身份证号</label>
        <div class="layui-input-block">
          <input type="text" name="title" required  lay-verify="required" placeholder="" autocomplete="off" class="layui-input" id="idcard">
        </div>
      </div>
      <div class="layui-form-item">
        <label class="layui-form-label">住址</label>
        <div class="layui-input-block">
          <input type="text" name="title" required  lay-verify="required" placeholder="" autocomplete="off" class="layui-input" id="address">
        </div>
      </div>
      <div class="layui-form-item">
        <label class="layui-form-label">有效期结束</label>
        <div class="layui-input-block">
          <input type="text" name="title" required  lay-verify="required" placeholder="" autocomplete="off" class="layui-input" id="endtime">
        </div>
      </div>
      <div class="layui-form-item" style="text-align: center">
        <div class="layui-input-block">
          <!-- <button class="layui-btn" lay-submit lay-filter="formDemo">获取身份证</button>
          <button type="reset" class="layui-btn layui-btn-primary">重置</button> -->
          <button type="button" class="layui-btn" id="get_info">获取身份证</button>
            <button type="button" class="layui-btn" id="print_info">打印</button>
        </div>
      </div>
    </form>
</div>

<script>
//由于模块都一次性加载，因此不用执行 layui.use() 来加载对应模块，直接使用即可：
;!function(){
    var layer = layui.layer
            ,form = layui.form;

    //监听提交
    var data = {
        idcard_name : "",
        idcard_sex: "",
        idcard_birthday: "",
        idcard_id: "",
        idcard_addr: "",
        idcard_end: "",
    }
    setInfo(data);

    // 读取身份证
    $("#get_info").on("click", function(){
      var isLink = checkHttpPrint();
      if( isLink != true ){
          layer.msg("打印插件未启用！");
          return false;
      }
      getIdcard();
    });
    
    // 打印
    $("#print_info").on("click", function(){
      var isLink = checkHttpPrint();
      if( isLink != true ){
          layer.msg("打印插件未启用！");
          return false;
      }
      var timestamp = new Date();
      var info = getInfo();
      var infoLog = getInfo2();
      info.gender = "";
      info.birthday = "";
      info.address = "";
      info.endtime = "";
      // info.createtime = getLocalTime(timestamp);
      
      var printerNo = "Gprinter GP-6024T";
      // var printerNo = "导出为WPS PDF";
      console.log(infoLog);
      // printLog(infoLog);
      $.ajax({
        url : "{url('printer/index')}",
        type : "post",
        dataType : "json",
        data : infoLog,
        success:function(res){
          if(res.status==1){
            info.createtime = res.data.createtime;
            printIdcard(info, printerNo);
          }
        }
      });
      
    });
}();

function printLog(info){
  $.ajax({
    url : "{url('printer/index')}",
    type : "post",
    dataType : "json",
    data : info,
    success:function(res){
      if(res.errcode==1){

      }
    }
  });
}

function getIdcardCallback(data){
    setInfo(data);
}
function getInfo(){
    var info = {
      realname : "姓名："+$("#realname").val(),
      idcard  : "身份证："+$("#idcard").val(),
      gender  : "性别："+$("#gender").val(),
      birthday  : "出生日期："+$("#birthday").val(),
      address  : "地址："+$("#address").val(),
      endtime  : "有效期至："+$("#endtime").val(),
    }
    return info;
}
function getInfo2(){
    var info = {
      realname : $("#realname").val(),
      idcard  : $("#idcard").val(),
      gender  : $("#gender").val(),
      birthday  : $("#birthday").val(),
      address  : $("#address").val(),
      endtime  : $("#endtime").val(),
    }
    return info;
}
function setInfo(data){
  $("#realname").val(data.idcard_name);
    $("#gender").val(data.idcard_sex);
    $("#birthday").val(data.idcard_birthday);
    $("#idcard").val(data.idcard_id);
    $("#address").val(data.idcard_addr);
    $("#endtime").val(data.idcard_end);
}
function clearInfo(){
  $("#realname").val("");
    $("#gender").val("");
    $("#birthday").val("");
    $("#idcard").val("");
    $("#address").val("");
    $("#endtime").val("");
}

//time  时间戳 
function getLocalTime(time){
    // time = time + 8 * 3600;
    // var d=new Date(time*1000);
    var d=new Date(time);
    var day=d.getUTCDate();
    var month=d.getUTCMonth() + 1;
    var year=d.getUTCFullYear();
    var hour=d.getHours();     
    var minute=d.getMinutes();     
    var second=d.getSeconds();   

    return year + "-" + month + "-" + day + " " + hour + ":" + minute + ":" +second;
    // return year + "-" + month + "-" + day;
}
</script>
<script src="/public/admin/js/printer.js"></script>
</body>

</html>